package frame

import "panel"
// import "fmt"

type FramePanel struct {
    panel.Common
    inside panel.View
    header panel.View
    scroll *panel.ScrollablePanel
}

type Null struct {
	panel.Common
}

func NewFrame(d panel.Doer, inside panel.View, scroll *panel.ScrollablePanel, header panel.View) panel.View {
    if header == nil { header = &Null{} }
    f := &FramePanel{inside: inside, header: header, scroll: scroll}
    if scroll != nil { scroll.Parent(f) }
    inside.Parent(f)
    header.Parent(f)
    return f
}

func (f *FramePanel) At(xy panel.Point) panel.View {
	// panel.Log("FramePanel.At: xy, wh = ", xy, f.WH)
    if 0 <= xy.X && xy.X < f.WH.X && 0 <= xy.Y && xy.Y < f.WH.Y {
    	// fmt.Printf( "FramePanel.At: selected %T\n", f.inside )
        return f.inside.At(xy.Sub(panel.Point{10, 10}))
    }
    // panel.Log("FramePanel.At: returning self")
    return f
}

func (f *FramePanel) FocusForClick(xy panel.Point) panel.View {
    if 0 <= xy.X && xy.X < f.WH.X && 0 <= xy.Y && xy.Y < f.WH.Y {
        return f.inside.FocusForClick(xy.Sub(panel.Point(f.WH)))
    }
    return f
}

func (f *FramePanel) Refresh() {
    f.inside.Refresh()
    f.header.Refresh()
    f.Box( panel.Point{}, panel.Point(f.WH) )
    // panel.Log("FramePanel.Refresh: wh = ", f.WH)
}

func (f *FramePanel) Cursor(xy panel.Point) {
    f.Common.Parental.Cursor(xy)
}

func (f *FramePanel) Key(rune int) panel.View {
    return nil
}

func (f *FramePanel) Resize(wh panel.Size, offset panel.Point) {
    f.Common.Resize(wh, offset)
    panel.Log( "FramePanel.Resize: offset", offset)
    const (DW = 18 * 256; DH = 28 * 256)
    f.inside.Resize( panel.Size(wh.Sub(panel.Size{DW, DH})), panel.Point{DW/2, DH/2})
    f.header.Resize(panel.Size(panel.Point{256 * 7 * 80, 256 * 13}), panel.Point{DW/2, 0})
    // if f.scroll != nil { f.scroll.Resize(DW, dy - DW, dx, dy + DH) }
}









